package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.RecruitManagerAdapter;
import com.wbao.dianniu.data.RecruitAddData;
import com.wbao.dianniu.data.RecruitListData;
import com.wbao.dianniu.listener.IRecruitListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.RecruitListChangeManager;
import com.wbao.dianniu.manager.RecruitListManager;
import com.wbao.dianniu.manager.RecruitListChangeManager.RecruitChangedListener;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

/**
 * 岗位管理
 * 
 * @author yejianjun
 * 
 */
public class RecruitManagerActivity extends BaseActivity  implements IRecruitListListener,OnClickListener,RecruitChangedListener{
	private PullToRefreshListView pullListView;
	private ListView mListView;
	private List<RecruitListData> mList;
	private RecruitManagerAdapter adapter;
	private RecruitListManager manager;
	private final int PAGE_COUNTS = 10;
	private int currentPage = 0;
	private TextView noDataTV;
	private Button rightBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_my_apply_recruit,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
		setTitleName(getResources().getString(R.string.post_manager));
		init();
		rightBtn = getRightTextButton();
		rightBtn.setText(getResources().getString(R.string.add));
		rightBtn.setOnClickListener(this);
		
		manager = new RecruitListManager(this);
		manager.addRecruitListener(this);
		reqData(currentPage);
		initData();
		RecruitListChangeManager.getInstance().addRecruitChangeListener(this);
	}
	
	private void init(){
		noDataTV = (TextView) findViewById(R.id.no_find_data);
		
		pullListView = (PullToRefreshListView) findViewById(R.id.apply_recruit_listView);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		mListView = pullListView.getRefreshableView();
		registerForContextMenu(mListView);
	}
	private void initData(){
		mList = new ArrayList<RecruitListData>();
		adapter = new RecruitManagerAdapter(this);
		mList = new ArrayList<>();
		adapter.setData(mList);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(itemClickListener);
	}
	
	OnRefreshListener onRefreshListener = new OnRefreshListener() {
		
		@Override
		public void onRefresh() {
			//  上拉刷新 加载请求数据
			pullUpRequest();
		}
	};
	
	// 上拉刷新数据请求
	private void pullUpRequest(){
		//没有搜索结果 或者数据为空 上拉没有效果
		++currentPage ;
		reqData(currentPage);
	}

	private void reqData(int page) {
		manager.recruitList(GlobalContext.accountId, 2, page * PAGE_COUNTS,PAGE_COUNTS, "", "", "", "");
	}
	OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.putExtra(GlobalContext.INTENT_JOB_ID, mList.get(position-1).getId());
			intent.setClass(RecruitManagerActivity.this, RecruitDetailsActivity.class);
			startActivity(intent);
		}
	};

	@Override
	public void onRecruitListFailure(int errorCode, String errorMsg) {
		pullListView.onRefreshComplete(false, false);
	}

	@Override
	public void onRecruitListSuccess(List<RecruitListData> list) {
		pullListView.onRefreshComplete(false, false);
		if(null != list){
			if(0 != currentPage){
				adapter.addData(list);
				mList = adapter.getList();
			}else{
				mList = list;
				adapter.setData(mList);
			}
			adapter.notifyDataSetChanged();
			if(mList.size() == 0){
				noDataTV.setVisibility(View.VISIBLE);
			}else{
				noDataTV.setVisibility(View.GONE);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.right_button:
			if(!GlobalContext.isIndustryAuthority){
				Notification.toast(this, "请先完成行业认证");
				return;
			}
			Intent intent = new Intent();
			intent.setClass(this, RecruitReleaseActivity.class);
			startActivity(intent);
			break;
		case R.id.back_button:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void recruitAddChanged(RecruitListData data) {
		noDataTV.setVisibility(View.GONE);
		mList.add(data);
		adapter.setData(mList);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void recruitRemoveChanged(int recruitId) {
		for (int i = 0; i < mList.size(); i++) {
			if(recruitId == mList.get(i).getId()){
				mList.remove(i);
				adapter.notifyDataSetChanged();
				if(mList.size() == 0){
					noDataTV.setVisibility(View.VISIBLE);
				}else{
					noDataTV.setVisibility(View.GONE);
				}
				break;
			}
		}
	}

	@Override
	public void recruitModifyChanged(int recruitId, RecruitAddData data) {
		// TODO Auto-generated method stub
		for (int i = 0; i < mList.size(); i++) {
			if(recruitId == mList.get(i).getId()){
				mList.get(i).setPost(data.getPost());
				mList.get(i).setTryout(data.getTryout());
				mList.get(i).setSalary(data.getSalary());
				mList.get(i).setAddress(data.getAddress());
				mList.get(i).setExperience(data.getExperience());
				mList.get(i).setEducation(data.getEducation());
				mList.get(i).setDescribe(data.getDescribe());
				adapter.notifyDataSetChanged();
				break;
			}
		}
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
